package com.winsafe.scanlibrary;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/**
 * This handles the Data Editing intent
 */
public class DataEditingPlugin extends BroadcastReceiver {
    private final static String TAG = "DataEditingPlugin";
    private static final String INTENT_ACTION_EDIT_DATA =
            "com.honeywell.decode.intent.action.EDIT_DATA";
    private static final String INTENT_ACTION_SCAN_RESULT =
            "com.honeywell.tools.action.scan_result";

    @Override
    public void onReceive(final Context context, Intent intent) {
        if (INTENT_ACTION_EDIT_DATA.equals(intent.getAction())) {
            Log.d(TAG, "Got intent for data editing");
            int version = intent.getIntExtra("version", 0);
            Log.d(TAG, "Version: " + Integer.toString(version));

            switch(version) {
                case 1: {
                    /*
                    These extras are available:
                    "version" = Data Editing Intent version (integer)
                    "aimId" = The AIM Identifier (string)
                    "charset" = The character set used to convert "dataBytes" to "data" string (string)
                    "codeId" = The Honeywell Symbology Identifier (string)
                    "data" = The barcode data as a string (string)
                    "dataBytes" = The barcode data as a byte array (byte array)
                    "timestamp" = The barcode timestamp (string)
                     */
                    String aimId = intent.getStringExtra("aimId");
                    String charset = intent.getStringExtra("charset");
                    String codeId = intent.getStringExtra("codeId");
                    String data = intent.getStringExtra("data");
                    byte[] dataBytes = intent.getByteArrayExtra("dataBytes");
                    String timestamp = intent.getStringExtra("timestamp");

                    Log.d(TAG, "Data: " + data);
                    Log.d(TAG, "Bytes: " + bytesToHexString(dataBytes));
                    Log.d(TAG, "Charset: " + charset);
                    Log.d(TAG, "AimId: " + aimId);
                    Log.d(TAG, "CodeId: " + codeId);
                    Log.d(TAG, "Timestamp: " + timestamp);

                    Intent scanResultIntent = new Intent(INTENT_ACTION_SCAN_RESULT);
                    scanResultIntent.putExtra("decode_rslt", data);
                    context.sendBroadcast(scanResultIntent);
                    break;
                }
                default: {
                    // Only the extra string "data" is available
                    String data = intent.getStringExtra("data");

                    Log.d(TAG, "Barcode: " + data);

                    Intent scanResultIntent = new Intent(INTENT_ACTION_SCAN_RESULT);
                    scanResultIntent.putExtra("decode_rslt", data);
                    context.sendBroadcast(scanResultIntent);
                    break;
                }
            }

            // Modify barcode data here.
            // Since we already handled the barcode data by broadcasting the data out
            // through a custom intent, we will prevent Scanners service from
            // further processing the data by returning an empty string back.
            // Otherwise, if the modified barcode data is not empty, the Scanners service
            // will try to wedge the barcode data.
            String edittedBarcodeData = "";

            // Return modified data for Data Collection Service to process
            // along with result code. Result code 1 is success, else failure.
            Bundle resultBundle = new Bundle();
            resultBundle.putString("data", edittedBarcodeData);

            final PendingResult result = goAsync();
            result.setResultExtras(resultBundle);
            result.setResultCode(1);
            result.finish();
        }
    }

    private String bytesToHexString(byte[] arr) {
        String s = "[";
        for(int i = 0; i < arr.length; i++) {
            s += "0x" + Integer.toHexString(arr[i]) + ", ";
        }
        s = s.substring(0, s.length() - 2) + "]";
        return s;
    }
}
